<?php

class UserService extends My_Service
{
	/**
	 * Validate input params
	 *
	 * @param array $array
	 * @return array
	 */
		
	public function login()
	{
		//validate username length
		if(empty($_POST['userName']))
		{
			$this->_errorList['userName'] = My_Message::get("S001|Username");
			return false;
		}
		
		//validate password
		if(empty($_POST['password']))
		{
			$this->_errorList['password'] = My_Message::get("S001|Password");
			return false;
		}
		
		/*$userDAO = new UserDAO();
		if(!$userDAO->checkLogin($_POST['userName'],$_POST['password']))
		{
			$this->_errorList['userName'] = My_Message::get("S010");
			return false;
		}*/
		$auth = Zend_Auth::getInstance();
		$authAdapter = Zend_Registry::get("authAdapter");
		$authAdapter->setIdentity($_POST['userName'])
					->setCredential($_POST['password']);
		$result = $auth->authenticate($authAdapter);
		if(!$result->isValid())
		{
			$this->_errorList['userName'] = My_Message::get("S010");
			return false;
		}
		return true;
	}
	
	public function checkLogin()
	{
		$ssLogin = new Zend_Session_Namespace('Zend_Auth');
		if(empty($ssLogin->user))
		{
			return false;
		}
		return true;
	}
	
	public function check()
	{
		//validate username length
		$userNameValdLength = new Zend_Validate_StringLength(5, 50);
		if(!$userNameValdLength->isValid($_POST['userName']))
		{
			$this->_errorList['userName'] = My_Message::get("S002|Username|5|50");
		}
		else
		{
			//validate username string
			$userNameValdAlnum = new Zend_Validate_Alnum();
			if(!$userNameValdAlnum->isValid($_POST['userName']))
			{
				$this->_errorList['userName'] = My_Message::get("S006|Username");
			}
			else
			{
				//check username exist
				if(self::checkUsernameExist($_POST['userName']))
				{
					$this->_errorList['userName'] = My_Message::get("S008|Username");
				}
			}
		}
	
		//validate password length
		$passValdLength = new Zend_Validate_StringLength(5, 50);
		if(!$passValdLength->isValid($_POST['password']))
		{
			$this->_errorList['password'] = My_Message::get("S002|Password|5|50");
		}
		else
		{
			//validate password string
			$passValdAlnum = new Zend_Validate_Alnum();
			if(!$passValdAlnum->isValid($_POST['password']))
			{
				$this->_errorList['password'] = My_Message::get("S006|Password");
				return false;
			}
		}
			
		//check password confirm
		$passconf = $_POST["passconf"];
		if(strcmp($passconf,$_POST['password']) != 0)
		{
			$this->_errorList["passconf"] = My_Message::get("S009");
			return false;
		}
		
		//validate email
		$emailVald = new Zend_Validate_EmailAddress();
		if(!$emailVald->isValid($_POST['email']))
		{
			$this->_errorList['email'] = My_Message::get("S007|Email");
			return false;
		}
		else
		{
			//check email exist
			if(self::checkEmailExist($_POST['email']))
			{
				$this->_errorList['email'] = My_Message::get("S008|Email");
				return false;
			}
		}
		
		return true;
	}
	
	public function checkUsernameExist($username)
	{
		$userDAO = new UserDAO();
		return $userDAO->checkUsernameExist($username);
	}
	
	public function checkEmailExist($email)
	{
		$userDAO = new UserDAO();
		return $userDAO->checkEmailExist($email);
	}
	
	/**
	 * Get user from post params
	 *
	 * @return UserDTO
	 */
	
	public function bind()
	{
		$user = new UserDTO();
		if(!empty($_POST['userId']))//case edit
		{
			$user = $this->load($_POST['userId']);
		}
		$user->userName = $_POST ['userName'];
		$user->password = $_POST ['password'];
		$user->email = $_POST ['email'];
		$user->status = $_POST ['status'];
		if(empty($_POST['userId']))//case insert
		{
			$user->creationTime = date ( "Y-m-d h:i:s" );
		}
		//$user->lastTime = date ( "Y-m-d h:i:s" );
		
		return $user;
	}
	
 	public function store()
    {
    	//check input data
    	if(!$this->check())
    	{
			return false;
    	}
    	
    	//bind data
    	$user = $this->bind();
    	
    	//save data
    	try{
    		$this->save($user);
    	}
    	catch (Zend_Exception $e) {
    		$this->_errorList[] = $e->getMessage();
    		return false;
    	}
    	return true;
    }

}

